package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.pojo.Member;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class MemberServiceImpl implements MemberService {

	@Autowired
	private MemberDao memberDao;

  	@Transactional
	@Override
	public void saveOrEdit(String telephone) {
		// 根据手机号获取会员信息
		Member member = memberDao.findByTelephone(telephone);
		if( member == null){
			// 不是会员，自动注册
			member = new Member();
			member.setPhoneNumber(telephone);
			member.setRegTime(new Date());
			// 调用Service保存会员
			memberDao.add(member);
		}
	}

	@Override
	public Map<String, Object> getMemberReport() {
		List<String> months = getMonthOfLastYear();
		List<Long> memberCount = new ArrayList<>();
		//统计每个月份对应的会员数量
		for (String month : months) {
			String firstDay = month +  "-01"; // 2020-01-01
            String lastDay = getThisMonthLastDay(firstDay);
            long count = memberDao.findCountBeforeByDate(lastDay);
			memberCount.add(count);
		}
		Map<String,Object> map = new HashMap<>();
		map.put("months", months);
		map.put("memberCount", memberCount);

		return map;
	}

	@Override
	public Map<String, Object> getMonthNumber(String beginMonth, String endMonth) {

		String[] splitbegin = beginMonth.split("-");

		int begin0 = Integer.parseInt(splitbegin[0]);
		int begin1 = Integer.parseInt(splitbegin[1]);

		String[] splitend = endMonth.split("-");
		int end0 = Integer.parseInt(splitend[0]);
		int end1 = Integer.parseInt(splitend[1]);

		if (begin0>end0){
			String sTemp = beginMonth;
			beginMonth = endMonth;
			endMonth = sTemp;

		}

		if (begin0==end0){
			if (begin1>end1){
				String sTemp = beginMonth;
				beginMonth = endMonth;
				endMonth = sTemp;
			}
		}

  		List<String> months = getMonthBetween(beginMonth,endMonth);
		for (String month : months) {
			System.out.println(month);
		}
		List<Long> memberCount = new ArrayList<>();
		//统计每个月份对应的会员数量
		for (String month : months) {
			String firstDay = month +  "-01"; // 2020-01-01
			String lastDay = getThisMonthLastDay(firstDay);
			long count = memberDao.findCountBeforeByDate(lastDay);
			memberCount.add(count);
		}
		Map<String,Object> map = new HashMap<>();
		map.put("months", months);
		map.put("memberCount", memberCount);

		return map;
	}


	private String getThisMonthLastDay(String firstDay){
		//2020-02-01 thisMonthFirstDay
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date firstDate = sdf.parse(firstDay);
			//创建日历对象
			Calendar cal = Calendar.getInstance();
			//设置为本月一号
			cal.setTime(firstDate);
			//调整为下月1号
			cal.add(Calendar.MONTH, 1);
			//调整为前一天(本月的最后一天)
			cal.add(Calendar.DAY_OF_MONTH, -1);

			String str = sdf.format(cal.getTime());
			return str;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 获取最近一年的月份
	 * @return
	 */
	private List<String> getMonthOfLastYear(){
		List<String> months = new ArrayList<>();
		//获取前12个月份
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MONTH, -12);
		for (int i = 0; i < 12; i++) {
			calendar.add(Calendar.MONTH, 1);
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
			String date = simpleDateFormat.format(calendar.getTime());
			months.add(date);
		}
		return months;
	}



	//获取两个月份之间的月份集合
	private List<String> getMonthBetween(String beginMonth, String endMonth){
		ArrayList<String> result = new ArrayList<String>();
		ArrayList<String> result1 = new ArrayList<String>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//格式化为年月

		Calendar min = Calendar.getInstance();
		Calendar max = Calendar.getInstance();
		try {
			min.setTime(sdf.parse(beginMonth));//2019-02-29
			min.add(Calendar.DAY_OF_MONTH, +1);//2019-03-01
			min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);

			max.setTime(sdf.parse(endMonth));//2020-03-31
			max.add(Calendar.DAY_OF_MONTH, +1);//2020-04-01
			max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Calendar curr = min;
		while (curr.before(max)) {
			result.add(sdf.format(curr.getTime()));
			curr.add(Calendar.MONTH, 1);
		}
		for (String s : result) {
			String substring = s.substring(0, s.lastIndexOf("-" ));
			result1.add(substring);
		}
		return result1;
	}

/*******************************************add by liuyu************************************************/
	//统计会员性别占比
	@Override
	public List<Map<String, Object>> findMemberSexCount() {
		return memberDao.findMemberSexCount();
	}
	//统计会员年龄占比
	@Override
	public List<Map<String, Object>> findMemberAgeCount() {
		return memberDao.findMenberAgeCount();
	}
/*******************************************************************************************************/

}